package cn.kattomy.shop.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.chainsaw.Main;

public class CategoryInsert {
	
	private String regex = ",[^,]*,";
	private String regex1 = "\\{.*\\}";
	
	public static void main(String[] args) throws IOException {
		
	}
	
	public Map<Integer, List<String>> CategoryInsertMethod() throws IOException{
		//二级分类的集合{,妈妈装,,大码女装,,职业套装,,婚纱礼服,,运动套装,,唐装旗袍,,情趣内衣,} list.size()一级分类的数量
		ArrayList<String> cList = getcList("C:\\Users\\kattomy\\Desktop\\temp.jsp");
		//二级分类的集合 ,情趣内衣, list.size()二级级分类的数量 list.size()一级分类的数量
		Map<Integer, List<String>> map = getcsList(cList);
		Integer key = 0;
		List<String> list = map.get(key);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		
		return map;
	}
	
	public ArrayList<String> getcList(String filePath) throws IOException {
		ArrayList<String> list = new ArrayList<String>();
		
		String encoding="utf8";
		//获取文件
		File file=new File(filePath);
		InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding); 
        //创建文件输入流  
        BufferedReader reader = new BufferedReader(read);  
        //建立字符串缓冲区，存储大文件中读取的数据  
//        StringBuffer buffer = new StringBuffer();  
        //读取每一行
        String line;
        Matcher matcher;
        while((line = reader.readLine()) != null){  
//        	buffer.append(line); 
//        	String[] fat = line.split(regex1);
//        	String[] sub = fat.toString().split(regex);
	        matcher = Pattern.compile(regex1).matcher(line);
	        while (matcher.find()) {
	        	list.add(matcher.group());
	        }
        }
        reader.close();
		return list;
	}
	
	public Map<Integer, List<String>> getcsList(ArrayList<String> cList){
		Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
		Matcher matcher;
		
		for (int i = 0; i < cList.size(); i++) {
			matcher = Pattern.compile(regex).matcher(cList.get(i));
			ArrayList<String> csList = new ArrayList<>();
			while (matcher.find()) {
				String temp = matcher.group();
				csList.add(temp.substring(1, temp.length()-1));
			}
			map.put(i, csList);
		}
		
		return map;
	}
	
}		